package com.npjscg.medicalpro.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;
import com.npjscg.medicalpro.config.AuthAccess;
import com.npjscg.medicalpro.controller.dto.HospitalDTO;
import com.npjscg.medicalpro.entity.Hospital;
import com.npjscg.medicalpro.service.IHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raote
 * @since 2022-07-04
 */
@Api(tags = "医院信息模块")
@RestController
@RequestMapping("/hospital")
public class HospitalController {

    @Autowired
    private IHospitalService hospitalService;

    @ApiOperation(value = "添加医院信息")
    @PostMapping("/addHospital")
    public Result addHospital(@RequestBody Hospital hospital){
        String hcode = hospital.getHcode();
        String rank = hospital.getHrank();
        String hospital1 = hospital.getHospital();
        if (StrUtil.isBlank(hcode)||StrUtil.isBlank(rank)||StrUtil.isBlank(hospital1))
            return Result.error("-1", "请发送医院的完整信息！");
        else
            hospitalService.addHospital(hospital);
        return Result.success();
    }

    @ApiOperation(value = "删除医院信息")
    @DeleteMapping("/deleteHospital/{hcode}")
    public Result deleteHospital(@PathVariable String hcode){
        if (StrUtil.isBlank(hcode))
            return Result.error("-1","发送hcode为空！");
        else
            hospitalService.deleteHospital(hcode);
        return Result.success();
    }

    @ApiOperation(value = "修改医院信息")
    @PostMapping("/editHospital")
    public Result editHospital(@RequestBody Hospital hospital){
        String hcode = hospital.getHcode();
        String rank = hospital.getHrank();
        String hospital1 = hospital.getHospital();
        if (StrUtil.isBlank(hcode)||StrUtil.isBlank(rank)||StrUtil.isBlank(hospital1))
            return Result.error("-1", "请发送医院的完整信息！");
        else
            hospitalService.editHospital(hospital);
        return Result.success();
    }

    @AuthAccess
    @ApiOperation(value = "分页查询医疗机构")
    @GetMapping("/hospitalPage")
    public Result hospitalPage(@RequestParam("pagenum") int pagenum, @RequestParam("pagesize") int pagesize,
    @RequestParam(value = "hospital",defaultValue = "",required = false) String hospital){
        Page<Hospital> page = new Page<>(pagenum,pagesize);
        return Result.success(hospitalService.hospitalPage(page, hospital));
    }

    @AuthAccess
    @ApiOperation(value = "分页查询医疗机构通过部门来查询！")
    @GetMapping("/hospitalPageByDep")
    public Result hospitalPageByDep(@RequestParam("pagenum") int pagenum, @RequestParam("pagesize") int pagesize,
                               @RequestParam(value = "did",defaultValue = "",required = false) Integer did){
        Page<Hospital> page = new Page<>(pagenum,pagesize);
        return Result.success(hospitalService.hospitalPageByDep(page, did));
    }



    @AuthAccess
    @ApiOperation(value = "获取医疗信息最新上报内容")
    @GetMapping("/checkOne/{hcode}")
    public Result checkOne(@PathVariable String hcode){
        if (StrUtil.isBlank(hcode))
            return Result.error("-1", "hcode为空！");
        else
            return Result.success(hospitalService.checkOne(hcode));
    }

    @ApiOperation(value = "根据医疗机构代码来查询医疗机构")
    @GetMapping("/search/{hcode}")
    public Result search(@PathVariable String hcode){
        Hospital byId = hospitalService.getById(hcode);
        if (byId == null)
            return Result.error(Constants.CODE_400, "数据库中没有此医疗机构信息");
        else
            return Result.success(byId);
    }

    @ApiOperation(value = "提供用户填写列表")
    @GetMapping(value = {"/findAll/{hospital}","/findAll"})
    public Result findAll(@PathVariable(required = false) String hospital){
        if (hospital == null)
            hospital = "";
        return Result.success(hospitalService.findAll(hospital));
    }

    @ApiOperation(value = "获取折线图所需数据")
    @GetMapping("/getChart/{hcode}")
    public Result getChart(@PathVariable String hcode){
        return Result.success( hospitalService.getChart(hcode));
    }

}
